<template>
  <div class="login">
    <div class="login-header">
      <h2 class="system-name">高考志愿填报系统</h2>
      <p class="slogan">科学规划 • 精准选择 • 助力未来</p>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">
        <img src="../assets/logo/logo.png" class="logo-image">
        <span class="system-title">高考志愿填报系统</span>
      </h3>
      <el-form-item prop="username">
        <el-input 
          v-model="loginForm.username" 
          type="text" 
          auto-complete="off" 
          placeholder="请输入账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <div class="remember-box">
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
      </div>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="register-link" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <div class="login-footer">
      <p>2025.5 高考志愿填报系统 版权所有mag</p>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ]
      },
      loading: false,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCookie();
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        console.log("进行登录操作")
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  position: relative;
}

.login-header {
  position: absolute;
  top: 50px;
  left: 50px;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  
  .system-name {
    font-size: 36px;
    margin-bottom: 10px;
    font-weight: bold;
  }
  
  .slogan {
    font-size: 18px;
    letter-spacing: 1px;
  }
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  
  .logo-image {
    width: 50px;
    height: 50px;
    position: relative; 
    top: 13px;
    right: 6px;
    transition: all 0.3s;
    
    &:hover {
      transform: scale(1.05);
    }
  }
  
  .system-title {
    color: #3370ff;
    font-size: 22px;
    font-weight: bold;
  }
}

.login-form {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.95);
  width: 400px;
  margin-left: 60%;
  padding: 35px 35px 15px 35px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  
  .el-input {
    height: 40px;
    
    input {
      height: 40px;
      padding-left: 35px;
      border-radius: 4px;
      transition: all 0.3s;
      border: 1px solid #dcdfe6;
      
      &:focus {
        border-color: #3370ff;
        box-shadow: 0 0 0 2px rgba(51, 112, 255, 0.2);
      }
    }
  }
  
  .input-icon {
    height: 16px;
    width: 16px;
    margin-left: 10px;
    color: #909399;
    z-index: 1;
  }
  
  .el-form-item {
    margin-bottom: 25px;
  }
}

.remember-box {
  margin-bottom: 25px;
  color: #606266;
}

.login-button {
  width: 100%;
  border-radius: 4px;
  background: #3370ff;
  border-color: #3370ff;
  height: 45px;
  font-size: 16px;
  transition: all 0.3s;
  
  &:hover, &:focus {
    background: #5186ff;
    border-color: #5186ff;
  }
}

.register-link {
  float: right;
  margin-top: 15px;
  
  .link-type {
    color: #3370ff;
    text-decoration: none;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

.login-footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
</style>
<!--<template>
  <div>
    <el-input v-model="url" type="text" style="width: 20%" /> &nbsp; &nbsp;
    <el-button @click="join" type="primary">连接</el-button>
    <el-button @click="exit" type="danger">断开</el-button>

    <br />
    <el-input type="textarea" v-model="message" :rows="9" />
    <el-button type="info" @click="send">发送消息</el-button>
    <br />
    <br />
    <el-input type="textarea" v-model="text_content" :rows="9" /> 返回内容
    <br />
    <br />
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: "ws://127.0.0.1:8080/websocket/message",
      message: "",
      text_content: "",
      ws: null,
    };
  },
  methods: {
    join() {
      const wsuri = this.url;
      this.ws = new WebSocket(wsuri);
      const self = this;
      this.ws.onopen = function (event) {
        self.text_content = self.text_content + "已经打开连接!" + "\n";
      };
      this.ws.onmessage = function (event) {
        self.text_content += event.data + "\n";
      };
      this.ws.onclose = function (event) {
        self.text_content = self.text_content + "已经关闭连接!" + "\n";
      };
    },
    exit() {
      if (this.ws) {
        this.ws.close();
        this.ws = null;
      }
    },
    send() {
      if (this.ws) {
        this.ws.send(this.message);
      } else {
        alert("未连接到服务器");
      }
    },
  },
};
</script>-->
